#!/bin/sh
HG=ssh://hg@rabbit-hg.eng.vmware.com

if [ "x$1" = "x" -o "x$2" = "x" ]; then
    echo "Usage: $0 <tag of previous release> <bugzilla username>"
    exit 1
fi

# C+P this from rabbitmq-public-umbrella Makefile
# last updated for release 3.1.0
PLUGIN_REPOS="\
              cowboy-wrapper \
              eldap-wrapper \
              erlang-rfc4627-wrapper \
              mochiweb-wrapper \
              sockjs-erlang-wrapper \
              rabbitmq-amqp1.0 \
              rabbitmq-auth-backend-ldap \
              rabbitmq-auth-mechanism-ssl \
              rabbitmq-consistent-hash-exchange \
              rabbitmq-erlang-client \
              rabbitmq-federation \
              rabbitmq-federation-management \
              rabbitmq-jsonrpc \
              rabbitmq-jsonrpc-channel \
              rabbitmq-jsonrpc-channel-examples \
              rabbitmq-management \
              rabbitmq-management-agent \
              rabbitmq-management-visualiser \
              rabbitmq-metronome \
              rabbitmq-web-dispatch \
              rabbitmq-mqtt \
              rabbitmq-shovel \
              rabbitmq-shovel-management \
              rabbitmq-stomp \
              rabbitmq-toke \
              rabbitmq-tracing \
              rabbitmq-web-stomp \
              rabbitmq-web-stomp-examples \
              toke \
              webmachine-wrapper"

REPOS="rabbitmq-public-umbrella \
       rabbitmq-java-client \
       rabbitmq-server \
       rabbitmq-codegen \
       $PLUGIN_REPOS"

echo Pulling from Mercurial...

for repo in $REPOS ; do
    hg clone $HG/$repo > /dev/null
    cd $repo
    hg log -r "p2(merge() and (::default - ::tag($1))) - branch(default)" --template "{branch}\n" | grep '^bug[0-9]\{5\}$' | sed 's/bug//' > ../$repo.bugs-merged
    hg log -r "head() - ::branch(default) - ::branch(present(junk))" --template "{branch}\n" | grep '^bug[0-9]\{5\}$' | sed 's/bug//' > ../$repo.bugs-unmerged
    cd ..
    rm -rf $repo
    printf "."
done

echo

cat *.bugs-merged | sort | uniq > all-bugs-merged
rm *.bugs-merged

cat *.bugs-unmerged | sort | uniq > all-bugs-unmerged
rm *.bugs-unmerged

# Dodgy bugs that break XML-RPC or exist in hg but not bz.
cat all-bugs-unmerged | grep -v 21899 | grep -v 22019 > all-bugs-unmerged0 && mv all-bugs-unmerged0 all-bugs-unmerged

echo Pulling from bugzilla...

perl bz.pl --login $2 --rememberlogin --bug_id "`cat all-bugs-merged | tr '\n' ' '`" > bz-merged

perl bz.pl --login $2 --rememberlogin --bug_id "`cat all-bugs-unmerged | tr '\n' ' '`" > bz-unmerged

./check-release-hygiene0
